package cn.hill4j.tool.spring.ext.mvc;

import cn.hill4j.tool.spring.ext.mvc.decrypt.RequestDecStrategy;
import cn.hill4j.tool.spring.ext.mvc.exception.RequestDesRegisterException;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
  * ClassName: RequestDecStrategyFactory
  * Description: 请求参数解密工厂
  * @author hillchen
  * Date: 2023/8/13
  * @version: 1.0
  */
public final class RequestDecStrategyFactory {
    private RequestDecStrategyFactory(){};
    private static Map<String, RequestDecStrategy> STRATEGY_MAP = new ConcurrentHashMap<>();
    /**
     * @author hillchen
     * Description: 注册请求参数解密策略
     * Date: 2023/8/13 00:44
     * @param strategyType 策略业务类型标志
     * @param strategy 策略处理器
     * @return void
     **/
    static void registerStrategy(String strategyType, RequestDecStrategy strategy){
        if (STRATEGY_MAP.containsKey(strategyType)){
            throw new RequestDesRegisterException(String.format("%s解析策略以及注册,不能重复注册",strategyType));
        }
        STRATEGY_MAP.putIfAbsent(strategyType, strategy);
    }
    /**
     * @author hillchen
     * Description: 获取请求参数解密策略
     * Date: 2023/8/13 00:45
     * @param strategyType 策略业务类型标志
     * @return void
     **/
    static RequestDecStrategy selectStrategy(String strategyType){
        return STRATEGY_MAP.get(strategyType);
    }
}
